package vip.xumy.admin.um.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import vip.xumy.admin.um.pojo.Menu;
import vip.xumy.admin.um.pojo.User;
import vip.xumy.admin.um.service.IMenuService;
import vip.xumy.admin.um.service.IUserService;
import vip.xumy.admin.utils.AdminLogUtil;
import vip.xumy.admin.utils.LoginUtil;
import vip.xumy.core.exception.CoreException;
import vip.xumy.core.pojo.com.AjaxResponse;

/**	Ownership belongs to the company
 *	author:mengyxu
 *	date:2019年5月10日
 */

@RestController
@RequestMapping("um")
public class LoginController {
	@Autowired
	private IUserService userService;
	@Autowired
	private IMenuService permissionService;
	
	/**
	 * 类型一用户登陆接口，登陆成功后用户信息保存在resdis中,将token信息保存在cookie中
	 */
	@RequestMapping(value = "sso/login", method = RequestMethod.POST)
	public AjaxResponse loginWithSso(@RequestBody User param, HttpServletRequest request,
			HttpServletResponse response) throws CoreException{
		String userId = param.getUserId();
		String key = param.getKey();
		User user = userService.findUser(userId,key);
		if(user == null){
			return new AjaxResponse(false, "用户名或密码错误！");
		}
		if("0".equals(user.getStatus())) {
			AjaxResponse rsp = new AjaxResponse(true, null);
			rsp.setData(0);
			return rsp;
		}
		userService.updateLogin(userId);
		//保存登陆信息
		LoginUtil.saveLoginInfo(user, request, response);
		//记录日志
		AdminLogUtil.Login(request, user);
		
		return new AjaxResponse(true, "登录成功！");
	}

	
	/**
	 * 注销登陆接口
	 */
	@RequestMapping(value = "login/out", method = RequestMethod.GET)
	public AjaxResponse loginOut(HttpServletRequest request) throws CoreException{
		//记录日志
		AdminLogUtil.Logout(request);
		
		LoginUtil.loginOut(request);
		
		return new AjaxResponse(true, "已销登陆！");
	}
	
	/**
	 * 获取登陆用户信息
	 */
	@RequestMapping(value = "user/info", method = RequestMethod.GET)
	public User getUserInfo(String token, HttpServletRequest request){
		User userInfo = LoginUtil.getUserInfo(token, request, true);
		return userInfo;
	}
	
	/**
	 * 检查登陆状态接口
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "check", method = RequestMethod.GET)
	public AjaxResponse checkSession(HttpServletRequest request) throws CoreException{
		User user = LoginUtil.getUserInfo(request);
		if(user != null){
			AjaxResponse rsp = new AjaxResponse();
			rsp.setSuccess(true);
			return rsp;
		}
		return new AjaxResponse(false, "session timeout");
	}
	
	/**
	 * 修改密码
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "update/password", method = RequestMethod.POST)
	public AjaxResponse updatePassword(@RequestBody User user, HttpServletRequest request) throws CoreException{
		userService.updateKey(user);
		AdminLogUtil.updatePassword(request, user);
		return new AjaxResponse(true, "修改密码成功！");
	}
	
	@RequestMapping(value = "menu/tree", method = RequestMethod.GET)
	public List<Menu> queryMenuTree(HttpServletRequest request){
		String userId = LoginUtil.getUserId(request);
		return permissionService.queryMenuTree(userId,null);
	}
	
}
